package QueueTest;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class pushTest {
    public static void main(String[] args) {
        /**
         * 推荐结果打散， 多少间隔才插入一个图片，
         * 最开始多少视频都顺序放，遇到图片判断间隔，间隔不大于的可以插入图片，
         * 间隔小于 等于 继续放视频
         */
        List<String> picAndVideo=new ArrayList<>();
        picAndVideo.add("v_0");
        picAndVideo.add("v_1");
        picAndVideo.add("v_2");
        picAndVideo.add("p_3");
        picAndVideo.add("p_4");
        picAndVideo.add("p_5");
        picAndVideo.add("v_6");
        picAndVideo.add("p_7");
        picAndVideo.add("v_8");
        picAndVideo.add("v_9");
        List<String> result= getRecommendResult(picAndVideo,2);
        for (String s : result) {
            System.out.println(s);
        }
    }
    public static List<String> getRecommendResult(List<String> picAndVideo, int maxInterval){
        List<String> result=new ArrayList<>();
        if (picAndVideo == null || picAndVideo.isEmpty()) {
            return result;
        }
        Queue<String> videoQueue=new LinkedList<>();
        Queue<String> picQueue=new LinkedList<>();
        boolean firstPic=false;
        int index=0;
        int picAndVideoSize=picAndVideo.size();
        while(!firstPic && index<picAndVideoSize){
            if (isVideo(picAndVideo.get(index))){
                result.add(picAndVideo.get(index));
                index++;
            }else{
                firstPic=true;
            }
        }
        while (index<picAndVideoSize){
            if (isVideo(picAndVideo.get(index))){
                videoQueue.add(picAndVideo.get(index));
            }else{
                picQueue.add(picAndVideo.get(index));
            }
            index++;
        }
        int currentSize = result.size();
        while(!videoQueue.isEmpty() && !picQueue.isEmpty()){
            if(currentSize>=maxInterval){
                result.add(picQueue.poll());
                currentSize=0;
            }else{
                result.add(videoQueue.poll());
                currentSize++;
            }
        }
        while(!videoQueue.isEmpty()){
            result.add(videoQueue.poll());
        }
        if (currentSize>=maxInterval && !picQueue.isEmpty()){
            result.add(picQueue.poll());

        }
        return result;
    }

    private static boolean isVideo(String s) {
        return s.contains("v");
    }
}
